<template>
  <div>
    <el-form :inline="true" :model="listQuery" class="filter-container">
      <el-form-item label="团队名称">
        <el-input
          v-model.trim="listQuery.teamName"
          placeholder="请输入团队名称"
          class="filter-item"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          size="small"
          round
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
          >搜索</el-button
        >
        <el-button
          class="filter-item"
          size="small"
          round
          icon="el-icon-plus"
          @click="handleCreate"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="listLoading" :data="list" highlight-current-row>
      <el-table-column label="名称" prop="teamName" min-width="200px" >
            <template slot-scope="{ row }">
              <span>
                <i class="iconfont el-icon-alltuanduiguanli"></i>
                {{ row.teamName}}
              </span>
            </template>
      </el-table-column>
      <el-table-column label="简称" prop="teamAbbr" width="200px" />
      <el-table-column label="成员数量" prop="memberSize" width="100px" />
      <el-table-column label="加入时间" prop="joinTime" width="160px" />
      <el-table-column align="center" width="60px">
        <template slot-scope="{ row }">
          <el-dropdown trigger="click">
            <i class="el-icon-more"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleDetails(row)">成员</el-dropdown-item>
              <el-dropdown-item @click.native="handleUpdate(row)">设置</el-dropdown-item>
                <el-dropdown-item @click.native="deleteTean(row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      :title="textMap[drawerStatus]"
      :visible.sync="showDrawer"
      direction="rtl"
    >
      <div>
        <el-form
          ref="dataForm"
          :rules="rules"
          :model="temp"
          label-position="right"
          label-width="100px"
        >
          <el-form-item label="团队名称" prop="teamName">
            <el-input v-model.trim="temp.teamName" />
          </el-form-item>
          <el-form-item label="团队简称" prop="teamAbbr">
            <el-input v-model.trim="temp.teamAbbr" />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="drawerStatus === 'create' ? createData() : updateData()"
              >提交</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { save, update, listJoined ,deleteTeam } from "@/api/uc/team";

export default {
  components:{

  },
  data() {
    const TYPE_TEAM = 1;
    return {
      list: null,
      listLoading: true,
      listQuery: {

      },
      showDrawer: false,
      drawerStatus: "",
      textMap: {
        update: "修改团队",
        create: "新增团队",
      },
      temp: {
        teamAbbr: '',
        teamName: ''
      },
      rules: {
        teamName: [
          { required: true, message: "团队名称不能为空", trigger: "change" },
        ],
        teamAbbr: [
          { required: true, message: "团队简称不能为空", trigger: "change" },
        ]
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      listJoined(this.listQuery)
        .then((response) => {
          this.list = response.data;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.getList();
    },
    handleDetails(row) {
      this.$router.push("/detail/" + row.teamId)
    },
    resetTemp() {
      this.temp = {
        teamId: null,
        teamName: null,
        teamAbbr: null,
      };
    },
    handleCreate() {
      this.resetTemp();
      this.drawerStatus = "create";
      this.showDrawer = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
    },
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          save(this.temp).then(() => {
            this.showDrawer = false;
            this.$message({
              message: "操作成功",
              type: "success",
            });
            this.handleFilter();
          });
        }
      });
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row);
      this.drawerStatus = "update";
      this.showDrawer = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp);
          update(tempData).then(() => {
            this.getList();
            this.showDrawer = false;
            this.$message({
              message: "操作成功",
              type: "success",
            });
          });
        }
      });
    },
    handleDelete(row, index) {
      this.$confirm("此操作将删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {})
        .catch(() => {
          // do nothing
        });
    },
    deleteTean(row){
      console.log(row,'222222');
      deleteTeam({teamId:row.teamId}).then(res=>{
        if(res.code==1){
          this.$message.success('删除成功')
            this.getList();
        }
      })
    }
  },
};
</script>

<style scoped>
</style>
